﻿using Oracle.ManagedDataAccess.Client;
using Server.Constant;
using Server.Enum;
using Server.OptExp;
using Server.Framework.Message;
using Server.Framework.Processor;
using Server.Framework.Service;
using Server.Framework.Socket;
using Server.Message.Request;
using Server.Message.Response;
using Server.Model;
using System.Data.Common;

namespace Server.Processor
{
    /// <summary>
    /// Author: Leil
    /// Description: 新增或编辑权限
    /// CreateTime: 2020/5/13 10:12:49
    /// </summary>
    [ProcessorAttr(MessageId.ADD_EDIT_PERMISSION)]
    class AddEditPermissionProcessor : IProcessor
    {

        public void Process(ProtobufAppSession session, RequestBaseMessage message)
        {
            if (message is AddEditPermissionReq msg)
            {
                OperationResp resp = new OperationResp(MessageId.OPT_RESP);
                if (msg.Permission == null)
                {
                    resp.Code = 500;
                    resp.OptType = (int)OptType.ADD_EDIT;
                    resp.Content = "权限不能为空";
                    session.Send(resp);
                    return;
                } else
                {
                    try
                    {
                        if (msg.Permission.ID != 0)
                        {
                            EditPermission(msg.Permission, session);
                        }
                        else
                        {
                            AddPermission(msg.Permission, session);
                        }
                    }
                    catch (CommonOptException e)
                    {
                        resp.Code = 500;
                        resp.OptType = (int)OptType.ADD_EDIT;
                        resp.Content = e.Message;
                        session.Send(resp);
                    }
                }
            }
        }


        /// <summary>
        /// 新增权限
        /// </summary>
        /// <param name="permission">权限</param>
        /// <param name="resp">消息</param>
        private void AddPermission(PermissionDao permission, ProtobufAppSession session)
        {
            if (string.IsNullOrEmpty(permission.PerCode)) {
                throw new CommonOptException("权限标识不能为空");
            }
            string sql = $@"INSERT INTO SYS_PERMISSION 
( ID, PER_CODE, STATUS, SORT, CREATOR, CREATE_TIME )
VALUES
(seq_sys_permission.NEXTVAL, '{permission.PerCode}', {permission.Status}, {permission.Sort}, 'ADMIN', SYSDATE)";
            
            DBService.Instance().ExecuteNonQuery(sql, res => {
                OperationResp resp = new OperationResp(MessageId.OPT_RESP);
                if (res > 0)
                {
                    resp.Code = 200;
                    resp.OptType = (int)OptType.ADD_EDIT;
                    resp.Content = "新增成功";
                    session.Send(resp);
                } else
                {
                    resp.Code = 500;
                    resp.OptType = (int)OptType.ADD_EDIT;
                    resp.Content = "新增失败";
                    session.Send(resp);
                }
            });
        }

        /// <summary>
        /// 编辑权限
        /// </summary>
        /// <param name="permission">权限</param>
        /// <param name="resp">消息</param>
        private void EditPermission(PermissionDao permission, ProtobufAppSession session)
        {
            string sql = $@"UPDATE SYS_PERMISSION 
SET PER_CODE = '{permission.PerCode}',
STATUS = {permission.Status},
SORT = {permission.Sort},
UPDATOR = 'USER',
UPDATE_TIME = SYSDATE
WHERE ID = {permission.ID}";

            DBService.Instance().ExecuteNonQuery(sql, res => {
                OperationResp resp = new OperationResp(MessageId.OPT_RESP);
                if (res > 0)
                {
                    resp.Code = 200;
                    resp.OptType = (int)OptType.ADD_EDIT;
                    resp.Content = "更新成功";
                    session.Send(resp);
                }
                else
                {
                    resp.Code = 500;
                    resp.OptType = (int)OptType.ADD_EDIT;
                    resp.Content = "更新失败";
                    session.Send(resp);
                }
            });
        }


    }
}
